/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog ,datepicker) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'pushmsg',
                model: {
                    url:_global.url.api + 'pushmsg/{id}',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'修改推送消息',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function() {

                                _this.data.title = $('#title').val();
                                if(_this.data.title==''){
                                    Alert({msg:'请传入标题',type:'error'});
                                    return false;
                                }
                                _this.data.url = $('#url').val();
                                if(_this.data.url==''){
                                    Alert({msg:'请传入链接',type:'error'});
                                    return false;
                                }

                                _this.data.receiver_type = $("input[name='receiver_type']:checked").val();

                                _this.data.pushed_at = $('#pushed_at').val();
                                if(_this.data.pushed_at==''){
                                    Alert({msg: '推送时间不能为空',type: 'error'});
                                    return false;
                                }
                                var now = new Date();
                                var pushed_at = new Date(Date.parse(_this.data.pushed_at.replace(/-/g,'/')));

                                //if ((pushed_at.getTime() - 60*5*1000) < now.getTime()) { //发布时间必须设定为5分钟以后
                                //    //Alert({msg: '发布时间必须设定为5分钟以后',type: 'error'});
                                //    //return false;
                                //}
                                //alert(_this.data.id);return false;
                                _this.data.id = _this.data.id;
                               if(model.put()){
                                   _this.collection.reload();
                               };
                            },cancel: function() {}
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function() {
                            WdatePicker({
                                el:this.id,
                                dateFmt:'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'消息推送',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function() {
                        _this.data.title = $('#title').val();
                        if(_this.data.title==''){
                            Alert({msg:'请传入标题',type:'error'});
                            return false;
                        }
                        _this.data.url = $("input[name='url']").val() ? $("input[name='url']").val() : $("select[name='url'] option:selected").val();//$('.url').val();
                        if(_this.data.url==''){
                            Alert({msg:'请传入链接',type:'error'});
                            return false;
                        }

                        _this.data.receiver_type = $("input[name='receiver_type']:checked").val();

                        _this.data.pushed_at = $('#pushed_at').val();
                        var now = new Date();
                        var pushed_at = new Date(Date.parse(_this.data.pushed_at.replace(/-/g,'/')));
                        if (pushed_at.getTime() < now.getTime()) { //发布时间必须设定为5分钟以后
                            Alert({msg: '发布时间必须大于当前时间',type: 'error'});
                            return false;
                        }
                        _this.data.users = $('#usersid').val();
                        _this.data.urltype = $("input[name='urltype']:checked").val();
                        _this.data.isNow = $("input[name='isNow']:checked").val();
                        _this.data.users = $('#usersid').val();
                        _this.data.urltype = $("input[name='urltype']:checked").val();
                        _this.data.isNow = $("input[name='isNow']:checked").val();
                        if(new Model({
                            url: _global.url.api + 'pushmsg',
                            data: _this.data
                        }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function() {}
                });

                _this.dialog.show();

                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });

                $('.long_date').on('click', function() {
                    WdatePicker({
                        el:this.id,
                        dateFmt:'yyyy-MM-dd HH:mm:ss'
                    });
                });

                $("input[name='receiver_type']").change(function(){
                    var type = $(this).val();
                    if(type == '3'){
                        $('#users').show();
                    }else{
                        $('#users').hide();
                    }
                })

                $("input[name='isNow']").change(function(){
                    var type = $(this).val();
                    if(type == '0'){
                        $('#pushtime').show();
                    }else{
                        $('#pushtime').hide();
                    }
                })

                $("input[name='urltype']").change(function(){
                    var type = $(this).val();
                    if(type == 'native'){
                        $('#native_id').show();
                        $('#url_address').hide();
                    }else{
                        $('#native_id').hide();
                        $('#url_address').show();
                    }
                })
            });



        },


        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function(img) {
            console.log('this.data',this.data);
            if(typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});